<template>
  <div>
    <div>
      <el-form :inline="true" :model="form">
        <el-button-group>
          <el-button type="primary" icon="plus" @click="addHandler"/>
          <el-button type="primary" icon="edit" @click="editHandler"/>
          <el-button type="primary" icon="delete" @click="delHandler"/>
        </el-button-group>
      </el-form>
    </div>

    <el-table :data="getSysFiles" height="300" border :stripe="true" :highlight-current-row="true"
              @current-change="tableRowChangeClickHandler"
              style="width: 100%">
      <el-table-column prop="combinationCode" label="组合代码" min-width="20%"/>
      <el-table-column prop="tradeDate" label="日期" min-width="40%"/>
      <el-table-column prop="accumulatedIncome" label="累计收益率" min-width="40%"/>

    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="getSysFileCurrentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="getFilesCount">
      </el-pagination>
    </div>

    <upload-form ref="uploadForm" :mode="formMode" :param="currentRecord" :code='combinationCode'/>


  </div>
</template>

<script type="text/babel">
  import {mapActions} from 'vuex'

  import historyStore from './bll/historyStore'
  import store from '../../store'

  (!store.state.historyStore) && store.registerModule('historyStore', historyStore)


  import UploadForm from './uploadForm.vue';


  export default {
    data() {
      return {
        form: {
          region: ''
        },
        combinationCode: null,
        formMode: null,
        currentRecord: null,
        pageSize: 10,
        pageIndex: 1,
        //form: {},
        copyData: '',
        qrData: '',
        privFileUrlFlag: false
      }
    },
    components: {UploadForm},
    computed: {
      getSysFiles() {
        return this.$store.state.historyStore.History;
      },
      getFilesCount() {
        return this.$store.state.historyStore.totalCount;
      },
      getSysFileCurrentPage() {
        return this.$store.state.historyStore.currentPage;
      }
    },

    mounted() {
      console.log('file upload page mounted.');
      this.initPage();
    },

    methods: {
      ...mapActions(['queryHistory', 'queryHistoryCount', 'editFile', 'delHistory']),

      initPage() {
        const combinationCode = this.$route.query.combinationCode;
        this.combinationCode = combinationCode
        this.queryHistory({pageIndex: this.pageIndex, pageSize: this.pageSize, combinationCode: combinationCode});
        this.queryHistoryCount({pageIndex: this.pageIndex, pageSize: this.pageSize, combinationCode: combinationCode});
      },

      openUploadDialogHandler() {
        this.$refs.uploadDialog.open();
      },
      addHandler() {
        this.$refs.uploadForm.enable();
        this.formMode = 'ADD';
      },
      editHandler() {
        if (!this.currentRecord) {
          this.$message.warning('请选择一条记录');
          return;
        }

        this.$refs.uploadForm.enable();
        this.formMode = 'EDIT';
      },
      delHandler() {
        if (!this.currentRecord) {
          this.$message.warning('请选择一条记录');
          return;
        }

        this.$confirm('确定删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delHistory({combinationId: this.currentRecord.combinationId}).then(() => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            // this.queryBanks()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

      },
      tableRowChangeClickHandler(currentRow, oldCurrentRow) {
        console.log(currentRow);
        this.currentRecord = currentRow;
        this.$refs.uploadForm.disable();
      },

      handleSizeChange(val) {
        console.log(`每页 ${val} 条`)
        this.pageSize = val;
        this.queryHistory({pageIndex: 1, pageSize: val, combinationCode: this.combinationCode});

      },
      handleCurrentChange(val) {
        console.log(arguments);
        console.log(`current${val}`);
        this.pageIndex = val;
        this.queryHistory({pageIndex: val, pageSize: this.pageSize, combinationCode: this.combinationCode});
      }
    }
  }
</script>

<style lang="scss">
  .download-url {
    padding: 5px;
  }

  .qrcode-wrapper {
    margin: auto;
  }
</style>

